package com.holly.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.holly.common.R;

/**
 * 创建人 wzj 创建时间 2017/2/14.
 */

public class GridChart extends View{
    // ////////////默认值////////////////
    /** 默认背景色 */
    public static final int DEFAULT_BACKGROUD = android.R.color.white;

    /** 默认XY轴字体大小 **/
    public static final int DEFAULT_AXIS_TITLE_SIZE = 22;

    /** 默认XY坐标轴颜色 */
    private static final int DEFAULT_AXIS_COLOR = R.color.Grid_Color;

    /** 默认经纬线颜色 */
    private static final int DEFAULT_LONGI_LAITUDE_COLOR = R.color.Grid_Color;

    /** 默认纬线数 */
    protected static final int DEFAULT_LATITUDE_NUM = 5;

    /** 默认经线数 +1最好和显示个数成倍数关系*/
    protected static final int DEFAULT_LOGITUDE_NUM = 3;

    /** 默认边框的颜色 */
    public static final int DEFAULT_BORDER_COLOR = R.color.Grid_Color;

    /** 默认虚线效果 */
    private static final PathEffect DEFAULT_DASH_EFFECT = new DashPathEffect(new float[] { 3, 3, 3,
            3 }, 1);


    /**顶部高度*/
    public static float CHART_TOP = 10;
    /**底部部高度*/
    public static float CHART_BOTTOM = 10;
    /**左侧默认预留高度*/
    public static float LEFT_WIDTH = 50;
    /**右侧默认预留高度*/
    public static float RIGHT_WIDTH = 10;

    // /////////////属性////////////////
    /** 背景色 */
    private int mBackGround;

    /** 坐标轴XY颜色 */
    private int mAxisColor;

    /** 经纬线颜色 */
    private int mLongiLatitudeColor;

    /** 虚线效果 */
    private PathEffect mDashEffect;

    /** 边线色 */
    private int mBorderColor;

    /** 顶部Titles高度 */
    protected float topTitleHeight;
    /** 底部预留高度 */
    protected float dowmTitleHeight;
    /** 左边预留宽度*/
    protected float leftWidth;
    /** 右边预留宽度*/
    protected float rightWidth;

    protected float longitudeSpacing;
    protected float latitudeSpacing;
    public interface OnTabClickListener {
        void onTabClick(int indext);
    }
    public void setOnTabClickListener(OnTabClickListener onTabClickListener) {
        mOnTabClickListener = onTabClickListener;
    }
    private OnTabClickListener mOnTabClickListener;
    public GridChart(Context context) {
        super(context);
    }

    public GridChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GridChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public GridChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }
    private void init() {
        mBackGround = DEFAULT_BACKGROUD;
        mAxisColor = DEFAULT_AXIS_COLOR;
        mLongiLatitudeColor = DEFAULT_LONGI_LAITUDE_COLOR;
        mDashEffect = DEFAULT_DASH_EFFECT;
        mBorderColor = DEFAULT_BORDER_COLOR;
        topTitleHeight = CHART_TOP;
        dowmTitleHeight = CHART_BOTTOM;
        leftWidth = LEFT_WIDTH;
        rightWidth = RIGHT_WIDTH;
        mOnTabClickListener = null;
    }

    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        setBackgroundResource(mBackGround);
        int viewHeight = getHeight();
        int viewWidth = getWidth();
        longitudeSpacing = (viewWidth - leftWidth-rightWidth)/(DEFAULT_LOGITUDE_NUM + 1);
        latitudeSpacing = (viewHeight - topTitleHeight - dowmTitleHeight)/(DEFAULT_LATITUDE_NUM + 1);
        // 绘制边框
        drawBorders(canvas, viewHeight, viewWidth);

        // 绘制经线
        drawLongitudes(canvas, viewHeight, longitudeSpacing);

        // 绘制纬线
        drawLatitudes(canvas, viewHeight, viewWidth, latitudeSpacing);
    }

    /**
     * 绘制边框
     * @param canvas
     * @param viewHeight
     * @param viewWidth
     */
    private void drawBorders(Canvas canvas, int viewHeight, int viewWidth) {
        Paint paint = new Paint();
        paint.setColor(mBorderColor);
        canvas.drawLine(leftWidth,dowmTitleHeight,leftWidth,viewHeight - topTitleHeight,paint);
        canvas.drawLine(leftWidth,dowmTitleHeight,viewWidth -rightWidth,dowmTitleHeight,paint);
        canvas.drawLine(leftWidth,viewHeight-topTitleHeight,viewWidth-rightWidth,viewHeight-topTitleHeight,paint);
        canvas.drawLine(viewWidth-rightWidth,dowmTitleHeight,viewWidth-rightWidth,viewHeight - topTitleHeight,paint);
    }

    /**
     * 绘制经线
     * @param canvas
     * @param viewHeight
     * @param longitudeSpacing
     */
    private void drawLongitudes(Canvas canvas,int viewHeight,float longitudeSpacing){
        Paint paint = new Paint();
        paint.setColor(mLongiLatitudeColor);
        for(int i = 1;i<= DEFAULT_LOGITUDE_NUM;i++){
            canvas.drawLine(leftWidth + longitudeSpacing * i,dowmTitleHeight,longitudeSpacing* i+ leftWidth,viewHeight-topTitleHeight,paint);
        }
    }

    /**
     * 绘制纬线
     * @param canvas
     * @param viewHeight
     * @param viewWidth
     * @param latitudeSpacing
     */
    private void drawLatitudes(Canvas canvas, int viewHeight, int viewWidth, float latitudeSpacing) {
        Paint paint = new Paint();
        paint.setColor(mLongiLatitudeColor);
        for(int i=1;i <= DEFAULT_LATITUDE_NUM;i++){
            canvas.drawLine(leftWidth,latitudeSpacing * i + dowmTitleHeight,viewWidth-rightWidth,latitudeSpacing * i + dowmTitleHeight,paint);
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        Rect rect = new Rect();
        getGlobalVisibleRect(rect);
        float x = event.getRawX();
        float y = event.getRawY();
        return false;
    }
    public int getLongiLatitudeColor() {
        return mLongiLatitudeColor;
    }

    public void setLongiLatitudeColor(int longiLatitudeColor) {
        mLongiLatitudeColor = longiLatitudeColor;
    }

    public int getBackGround() {
        return mBackGround;
    }

    public void setBackGround(int backGround) {
        mBackGround = backGround;
    }

    public int getAxisColor() {
        return mAxisColor;
    }

    public void setAxisColor(int axisColor) {
        mAxisColor = axisColor;
    }

    public int getBorderColor() {
        return mBorderColor;
    }

    public void setBorderColor(int borderColor) {
        mBorderColor = borderColor;
    }

    public float getTopTitleHeight() {
        return topTitleHeight;
    }

    public void setTopTitleHeight(float topTitleHeight) {
        this.topTitleHeight = topTitleHeight;
    }

    public float getDowmTitleHeight() {
        return dowmTitleHeight;
    }

    public void setDowmTitleHeight(float dowmTitleHeight) {
        this.dowmTitleHeight = dowmTitleHeight;
    }

    public float getLeftWidth() {
        return leftWidth;
    }

    public void setLeftWidth(float leftWidth) {
        this.leftWidth = leftWidth;
    }

    public float getRightWidth() {
        return rightWidth;
    }

    public void setRightWidth(float rightWidth) {
        this.rightWidth = rightWidth;
    }
}
